package com.jdk.demo.unsafe.simple;

import java.lang.reflect.Field;

import sun.misc.Unsafe;

public class AllocateInstance {

	public static void main(String[] args) throws Exception {
        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe) f.get(null);

        A o1 = new A(); // constructor
        o1.a(); // prints 1
        A o2 = A.class.newInstance(); // reflection
        o2.a(); // prints 1
        A o3 = (A) unsafe.allocateInstance(A.class); // unsafe
        // allocateInstance()根本没有进入构造方法，对于单例模式，简直是噩梦
        o3.a(); // prints 0
    }

}
